* makes Figures 2 and 3
* this .do file uses the command wtmean, which is included in the Stata package _gwtmean from http://fmwww.bc.edu/RePEc/bocode/_

set more off
clear
capture log close

* SET DIRECTORY HERE 
cd "C:\Users\mitch\Dropbox\BLW\POP\Searching for a Bright Line\Searching for a Bright Line replication materials" 

log using "Logs/BLW-figures 2,3.log", replace text

use "Data/BLW_Expert_Waves123456.dta", clear

keep if wave==3
replace expert=1 if expert==.
gen weight=1 /*no weights on expert data*/
rename luth_id caseid /*to match YouGov*/

preserve
use "Data/BLW_Public_Waves3456.dta", clear

keep if wave==3
gen expert=0
tostring caseid, replace
save "Data/BLW_Public_Waves3456-w3.dta", replace
restore

append using "Data/BLW_Public_Waves3456-w3.dta"
* rm "Data/BLW_Public_Waves3456-w3.dta"

preserve
*reshape to create the single variable imp_ indicating importance of each statement
drop perf_*
keep if wave==3
reshape long imp_, i(caseid) j(statement) string

*substitute in clean labels
drop if statement=="restraint" | statement=="democracy_today" | statement=="science"
replace statement="All parties allowed" if statement=="ban_ideology"
replace statement="Free speech" if statement=="free_speech"
replace statement="Fraud-free elections" if statement=="election_fraud"
replace statement="Protest tolerated" if statement=="protest"
replace statement="Judiciary can limit executive" if statement=="judiciary_check"
replace statement="Opinions heard on policy" if statement=="opinions"
replace statement="Judicial independence" if statement=="judicial_ind" | statement=="judicial_independence"
replace statement="No interference with press" if statement=="journalists"
replace statement="Agencies do not punish" if statement=="monitor_opponents"
replace statement="No political violence" if statement=="private_violence"
replace statement="Legislature can limit executive" if statement=="legislature_check"
replace statement="Investigations not compromised" if statement=="investigations"
replace statement="Sanctions for misconduct" if statement=="misconduct"
replace statement="Equal political/legal rights" if statement=="equal_rights"
replace statement="Equal voting rights" if statement=="voting_rights"
replace statement="No foreign influence" if statement=="foreign_influence"
replace statement="No private gains from office" if statement=="private_gain"
replace statement="Campaign funds transparent" if statement=="funds_transparent" 
replace statement="Votes have equal impact" if statement=="votes_impact"
replace statement="Patriotism not questioned" if statement=="patriotism"
replace statement="Common understanding of facts" if statement=="common_und" | statement=="common_understanding"
replace statement="Contribs not determine policy" if statement=="contributions" | statement=="contributions_determine"
replace statement="Participation high" if statement=="participation"
replace statement="Compromise sought" if statement=="compromise"
replace statement="Districts not biased" if statement=="districts"
replace statement="Constitution limits executive" if statement=="expand_executive"
replace statement="Candidates disclose info" if statement=="candidates_disclose" 

*create binary measure
gen binaryimp=.
replace binaryimp=0 if (imp_==0 | imp_==1)
replace binaryimp=1 if (imp_==2 | imp_==3)

*create ordering variable
bysort wave statement: egen meanbinaryimp=mean(binaryimp) if expert==1
bysort statement: egen meanbinaryimp2=mean(meanbinaryimp) if expert==1

bysort wave statement: egen meanimppub=wtmean(binaryimp) if expert==0 & wave==3
bysort statement: egen meanimppub2=mean(meanimppub) if expert==0 

gen trumpapprove=.
replace trumpapprove=0 if approve_trmp==3 | approve_trmp==4
replace trumpapprove=1 if approve_trmp==1 | approve_trmp==2

gen trumpapprove2=trumpapprove
replace trumpapprove2=2 if expert==1

replace wt=100 if expert==1

separate binaryimp, by(trumpapprove2)

egen statementnum=group(statement)

gen statementmeandisapprove=.
gen disapprovell=.
gen disapproveul=.
forvalues i = 1/27 { 
svy: mean binaryimp if statementnum == `i' & wave == 3 & trumpapprove==0
matrix A=r(table)
local blah=A[1,1]
replace statementmeandisapprove = `blah' if statementnum == `i' 
local blah2=A[5,1]
replace disapprovell = `blah2' if statementnum == `i' 
local blah3=A[6,1]
replace disapproveul = `blah3' if statementnum == `i' 
} 

gen statementmeanapprove=.
gen approvell=.
gen approveul=.
forvalues i = 1/27 { 
svy: mean binaryimp if statementnum == `i' & wave == 3 & trumpapprove==1
matrix A=r(table)
local blah=A[1,1]
replace statementmeanapprove = `blah' if statementnum == `i' 
local blah2=A[5,1]
replace approvell = `blah2' if statementnum == `i' 
local blah3=A[6,1]
replace approveul = `blah3' if statementnum == `i' 
} 

gen publicimp=.
forvalues i = 1/27 { 
svy: mean binaryimp if statementnum == `i' & wave == 3 & expert==0
matrix A=r(table)
local blah=A[1,1]
replace publicimp = `blah' if statementnum == `i' 
}

collapse (mean) disapprovell disapproveul statementmeandisapprove approvell approveul statementmeanapprove publicimp, by(statement)

gsort +publicimp
gen ordernum=_n
export delimited statement using "Figures/fig3ylabels.csv", replace /*use this to generate label order*/
list statement publicimp ordernum
 
gen disapprovefake=.
gen approvefake=.
 

*Figure 3
twoway (scatter ordernum statementmeandisapprove,ylabel(1(1)27,grid) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%",labsize(*.8)) ///
msymbol(T) mcolor(blue)) (rspike disapprovell disapproveul ordernum, horizontal lcolor(blue) scheme(s2color) legend(row(1) pos(6) order(5 6) size(*.7) ///
label(5 "Disapprove of Trump") label(6 "Approve of Trump") symxsize(*.5)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
xtitle(`"Percentage who answered statement is "important" or "essential""' "for democratic government", size(*.7))) ///
(scatter ordernum statementmeanapprove, msymbol(Sh) mcolor(red)) (rspike approvell approveul ordernum, horizontal lcolor(red)) ///
(connected ordernum disapprovefake, xscale(r(0 1)) msymbol(T) mcolor(blue) lcolor(blue)) ///
(connected ordernum approvefake, xscale(r(0 1)) msymbol(Sh) mcolor(red) lcolor(red) ///
ylabel(27 "Fraud-free elections" 26 "Equal voting rights" 25 "Equal political/legal rights" 24 "Candidates disclose info" ///
23 "Investigations not compromised" 22 "Sanctions for misconduct" 21 "No foreign influence" 20 "Constitution limits executive" ///
19 "Opinions heard on policy" 18 "Votes have equal impact" 17 "Judicial independence" 16 "Protest tolerated" 15 "No private gains from office" ///
14 "Agencies do not punish" 13 "All parties allowed" 12 "Free speech" 11 "Legislature can limit executive" 10 "Judiciary can limit executive" ///
9 "Common understanding of facts" 8 "Participation high" 7 "Districts not biased" 6 "No interference with press" 5 "Campaign funds transparent" ///
4 "Contribs not determine policy" 3 "No political violence" 2 "Compromise sought" 1 "Patriotism not questioned", angle(0) labsize(*.75)) ytitle(""))

graph export "Figures/fig3.png", width(2000) replace
* graph export "Figures/fig3.tif", width(10000) replace

restore


preserve
*reshape to create the single variable imp_ indicating importance of each statement
drop perf_*
keep if wave==3
reshape long imp_, i(caseid) j(statement) string

*substitute in clean labels
drop if statement=="restraint" | statement=="democracy_today" | statement=="science"
replace statement="All parties allowed" if statement=="ban_ideology"
replace statement="Free speech" if statement=="free_speech"
replace statement="Fraud-free elections" if statement=="election_fraud"
replace statement="Protest tolerated" if statement=="protest"
replace statement="Judiciary can limit executive" if statement=="judiciary_check"
replace statement="Opinions heard on policy" if statement=="opinions"
replace statement="Judicial independence" if statement=="judicial_ind" | statement=="judicial_independence"
replace statement="No interference with press" if statement=="journalists"
replace statement="Agencies do not punish" if statement=="monitor_opponents"
replace statement="No political violence" if statement=="private_violence"
replace statement="Legislature can limit executive" if statement=="legislature_check"
replace statement="Investigations not compromised" if statement=="investigations"
replace statement="Sanctions for misconduct" if statement=="misconduct"
replace statement="Equal political/legal rights" if statement=="equal_rights"
replace statement="Equal voting rights" if statement=="voting_rights"
replace statement="No foreign influence" if statement=="foreign_influence"
replace statement="No private gains from office" if statement=="private_gain"
replace statement="Campaign funds transparent" if statement=="funds_transparent" 
replace statement="Votes have equal impact" if statement=="votes_impact"
replace statement="Patriotism not questioned" if statement=="patriotism"
replace statement="Common understanding of facts" if statement=="common_und" | statement=="common_understanding"
replace statement="Contribs not determine policy" if statement=="contributions" | statement=="contributions_determine"
replace statement="Participation high" if statement=="participation"
replace statement="Compromise sought" if statement=="compromise"
replace statement="Districts not biased" if statement=="districts"
replace statement="Constitution limits executive" if statement=="expand_executive"
replace statement="Candidates disclose info" if statement=="candidates_disclose" 

*create binary measure
gen binaryimp=.
replace binaryimp=0 if (imp_==0 | imp_==1)
replace binaryimp=1 if (imp_==2 | imp_==3)

*create ordering variable
bysort wave statement: egen meanbinaryimp=mean(binaryimp) if expert==1
bysort statement: egen meanbinaryimp2=mean(meanbinaryimp) if expert==1

bysort wave statement: egen meanimppub=mean(binaryimp) if expert==0 & wave==3 
bysort statement: egen meanimppub2=mean(meanimppub) if expert==0 

gen trumpapprove=.
replace trumpapprove=0 if approve_trmp==3 | approve_trmp==4
replace trumpapprove=1 if approve_trmp==1 | approve_trmp==2

gen trumpapprove2=trumpapprove
replace trumpapprove2=2 if expert==1

replace wt=100 if expert==1

gen newimp=binaryimp
separate newimp, by(expert)

egen statementnum=group(statement)

gen statementmeanpublic=.
gen publicll=.
gen publicul=.
forvalues i = 1/27 { 
svy: mean binaryimp if statementnum == `i' & wave == 3 & expert==0
matrix A=r(table)
local blah=A[1,1]
replace statementmeanpublic = `blah' if statementnum == `i' 
local blah2=A[5,1]
replace publicll = `blah2' if statementnum == `i' 
local blah3=A[6,1]
replace publicul = `blah3' if statementnum == `i' 
} 

gen statementmeanexpert=.
gen expertll=.
gen expertul=.
forvalues i = 1/27 { 
mean binaryimp if statementnum == `i' & wave == 3 & expert==1
matrix A=r(table)
local blah=A[1,1]
replace statementmeanexpert = `blah' if statementnum == `i' 
local blah2=A[5,1]
replace expertll = `blah2' if statementnum == `i' 
local blah3=A[6,1]
replace expertul = `blah3' if statementnum == `i' 
} 

collapse (mean) publicll publicul statementmeanpublic expertll expertul statementmeanexpert, by(statement)

gsort +statementmeanexpert
gen ordernum=_n
export delimited statement using "Figures/fig2ylabels.csv", replace /*use this to generate label order*/
 
gen publicfake=.
gen expertfake=.
 
*Figure 2
twoway (scatter ordernum statementmeanpublic,ylabel(1(1)27,grid) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%",labsize(*.8)) ///
msymbol(d) mcolor(purple)) (rspike publicll publicul ordernum, horizontal lcolor(purple) scheme(s2color) legend(row(1) pos(6) order(5 6) size(*.7) ///
label(5 "Public") label(6 "Experts") symxsize(*.5)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) xtitle(`"Percentage who answered statement is "important" or "essential""' "for democratic government", ///
size(*.7))) (scatter ordernum statementmeanexpert, msymbol(Oh) mcolor(green)) (rspike expertll expertul ordernum, horizontal lcolor(green)) ///
(connected ordernum publicfake, xscale(r(0 1)) msymbol(d) mcolor(purple) lcolor(purple)) (connected ordernum expertfake, xscale(r(0 1)) msymbol(Oh) mcolor(green) ///
lcolor(green) ylabel(27 "Fraud-free elections" 26 "Protest tolerated" 25 "Equal political/legal rights" 24 "No interference with press" ///
23 "Agencies do not punish" 22 "Free speech" 21 "Equal voting rights" 20 "Investigations not compromised" 19 "Opinions heard on policy" ///
18 "No political violence" 17 "Judicial independence" 16 "Sanctions for misconduct" 15 "All parties allowed" 14 "Legislature can limit executive" ///
13 "Judiciary can limit executive" 12 "Constitution limits executive" 11 "Candidates disclose info" 10 "Votes have equal impact" 9 "No foreign influence" ///
8 "Districts not biased" 7 "No private gains from office" 6 "Campaign funds transparent" 5 "Contribs not determine policy" 4 "Common understanding of facts" ///
3 "Participation high" 2 "Compromise sought" 1 "Patriotism not questioned", angle(0) labsize(*.75)) ytitle(""))

graph export "Figures/fig2.png", width(2000) replace
* graph export "Figures/fig2.tif", width(10000) replace

restore

log close
